#!/usr/bin/python3

import os
import re
import subprocess
import sys
import time

if __name__ == "__main__":
    OPENEULER_VERSION="22.03-LTS"
    FEDORA_VERSION="36"

    WORK_DIR=subprocess.run(["pwd"],stdout=subprocess.PIPE,encoding="utf-8").stdout
    WORK_DIR=WORK_DIR[0:len(WORK_DIR)-len("autotools/")]
    WGET_DIR="https://repo.oepkgs.net/openEuler/rpm/openEuler-22.03-LTS/compatible/f36/source/Packages/"

    #WGET_DIR="https://api.compass-ci.openeuler.org/rpm/testing/openeuler-22.03-LTS/compatible/f36/source/Packages/"
    print("\nPWD is:"+WORK_DIR+"  FEDORA_VERSION is:"+FEDORA_VERSION+"\n")

    
    
      
   

    Firstcode="exsit"
    FILE_NAME="index.html"
    indexurl=WGET_DIR
    subprocess.run(["rm",WORK_DIR+Firstcode+"/"+FILE_NAME])
    subprocess.run(["wget","-P"+WORK_DIR+Firstcode,indexurl])

    htmlfile=open(WORK_DIR+Firstcode+"/"+FILE_NAME,'r')
    pkgname_str=[]
    result=[]
    pkgname=[]

    lineslist=htmlfile.readlines()
    for line in lineslist:
        #print(line)l
        for i in range(len(line)) :
            if line[i:i+len(".src.rpm")]==".src.rpm" :
                for j in range(len(line)) :
                    if line[j-5:j] == "href=" :
                        pkgname_str.append(line[j+1:i])
                        #print(line[j+1:i+7])
                        break
                break
            
    htmlfile.close()


    #保存文件
    #subprocess.run(["mkdir",Firstcode])
    resultfile=open(WORK_DIR+Firstcode+"/"+OPENEULER_VERSION+"_"+Firstcode+"_pkglist",'w')

    for i in range(len(pkgname_str)):
        resultfile.write(pkgname_str[i]+(60-len(pkgname_str[i]))*" "+"fc"+FEDORA_VERSION+(10-len("fc"+FEDORA_VERSION))*" "+"Waiting"+"\n")
    resultfile.close()

    print("------------------------------------Tast Over--------------------------------------")
    print("Packages number:"+str(len(pkgname_str))+"\n")
    subprocess.run(["cat",WORK_DIR+"/"+Firstcode+"/"+Firstcode+"_pkglist"])
    print("\nPackages number:"+str(len(pkgname_str))+"\n")

    print("生成文件:"+WORK_DIR+Firstcode+"/"+Firstcode+"_pkglist")

    print('\n')

    time.sleep(1)


